class RequisicaosController < ApplicationController
  before_filter :autoriza_admin, :only => [:edit, :update, :destroy]
  # GET /requisicaos
  # GET /requisicaos.json
  def index
    if params[:hospedagem_id]
      @requisicaos = Requisicao.where("hospedagem_id = ?", params[:hospedagem_id])
    else
      @requisicaos = Requisicao.all
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @requisicaos }
    end
  end

  # GET /requisicaos/1
  # GET /requisicaos/1.json
  def show
    @requisicao = Requisicao.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @requisicao }
    end
  end

  # GET /requisicaos/new
  # GET /requisicaos/new.json
  def new
    @requisicao = Requisicao.new
    @hospedagem = Hospede.find(current_user.id).hospedagem_atual
    @servico = Servico.find(params[:servico_id])

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @requisicao }
    end
  end

  # GET /requisicaos/1/edit
  def edit
    @requisicao = Requisicao.find(params[:id])
  end

  # POST /requisicaos
  # POST /requisicaos.json
  def create
    @requisicao = Requisicao.new(params[:requisicao])
    logger.debug "Conteudo de params: #{params}"

    respond_to do |format|
      if @requisicao.save
        format.html { redirect_to @requisicao, notice: 'Requisicao was successfully created.' }
        format.json { render json: @requisicao, status: :created, location: @requisicao }
      else
        format.html { render action: "new", :servico_id => params[:requisicao][:servico_id], :estornado => 0 }
        format.json { render json: @requisicao.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /requisicaos/1
  # PUT /requisicaos/1.json
  def update
    @requisicao = Requisicao.find(params[:id])

    respond_to do |format|
      if @requisicao.update_attributes(params[:requisicao])
        format.html { redirect_to @requisicao, notice: 'Requisicao was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @requisicao.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /requisicaos/1
  # DELETE /requisicaos/1.json
  def destroy
    @requisicao = Requisicao.find(params[:id])
    @requisicao.destroy

    respond_to do |format|
      format.html { redirect_to requisicaos_url }
      format.json { head :no_content }
    end
  end
end